<?php
/**
 * @version $Id: view.html.php 48 2011-09-28 06:47:49Z lnmtrung $
 */
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewCpoint extends WrdView{
	
	function display($tpl = null) {
		$user  = &JFactory::getUser();
		if(!$user->authorise('wrd.dashboard.config', 'com_wrd'))
			return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
		
		$this->setModel($this->getModel('cpoint'), true);
		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc)))
			$this->$fnc($tpl);
		else
			parent::display($tpl);
	}

	function displayDefault($tpl = null){
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$model_projects	=& JModelLegacy::getInstance('projects','WrdModel');
		$model_clubs	=& JModelLegacy::getInstance('clubs','WrdModel');
		$model_outlets	=& JModelLegacy::getInstance('outlets','WrdModel');
		
		$document = & JFactory::getDocument();
		$document->setTitle( JText::_('Points Manager') );

		JToolBarHelper::title(JText::_('Points Manager'),'cpoint.png');
 		JToolBarHelper::addNew();
 		JToolBarHelper::publish();
 		JToolBarHelper::unpublish();
		JToolBarHelper::deleteList();
		
		WrdHelper::addSubMenu($view);
		$this->sidebar = JHtmlSidebar::render();

		$filter_order     		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_item','filter_order','id' );
		$filter_order_Dir 		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir','filter_order_Dir', '' );

		$lists['order'] 		= $filter_order;
		$lists['order_Dir'] 	= $filter_order_Dir;
		
		$items					= & $this->get( 'Data');
		
		$total					= & $this->get( 'Total');
		$pagination 			= & $this->get( 'Pagination' );
		 
		$search 				= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string' );
		$filter_state 			= $mainframe->getUserStateFromRequest( $context.$view.'published','published',-1,'int' );
		$filter_user			= $mainframe->getUserStateFromRequest( $context.$view.'created_id','created_id',-1,'int' );
		
		$search 				= JString::strtolower( $search );
		
		$lists['search'] 		= $search;
		$lists['published'] 	= JHTML::_('wrd.published',$filter_state,'onchange="this.form.submit()"');
		$lists['user'] 			= JHTML::_('wrd.listUser','created_id',$filter_user,'onchange="this.form.submit()"','clubs');
		$p_name = array() ;
		$c_name = array() ;
		$o_name = array() ;
		foreach ( $items as $item ){
			$project_name =  $model_projects->getNameByCode($item->p_code) ;
			$club_name = $model_clubs->getNameByCode($item->p_code,$item->c_code) ;
			$outlet_name = $model_outlets->getNameByCode($item->p_code,$item->c_code,$item->o_code) ;
			$p_name[$item->id] = $project_name->name ;
			$c_name[$item->id] = $club_name->name ;
			$o_name[$item->id] = $outlet_name->name ;
			//echo $item->p_code."/".$item->c_code."/".$item->o_code ;
		}
		$this->assignRef('p_name',		$p_name);
		$this->assignRef('c_name',		$c_name);
		$this->assignRef('o_name',		$o_name);
		
	    $this->assignRef('search',		$search);
	    $this->assignRef('lists',		$lists);
	  	$this->assignRef('items',		$items);
	    $this->assignRef('pagination',	$pagination);
		
		parent::display($tpl);
  	}
  
  	function displayForm($tpl = null){
  		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$user = JFactory::getUser();
		$cid = JRequest::getInt('id');
		$model	=& $this->getModel();
		$detail = array();
		
		if($cid != 0){
			$detail	= &$model->getItem($cid);
			$user->currentpoint = $detail;
		}
		
		$lists = array();
			
		$filter_pid 	= $mainframe->getUserStateFromRequest($context.$view.'p_code','p_code',$detail->p_code,'char');
		$javascript = "onchange=\"changeClubs('c_code', sectionprojects,document.adminForm.p_code.options[document.adminForm.p_code.selectedIndex].value , 0, 0);\"";
		$filter_cid 	= $mainframe->getUserStateFromRequest($context.$view.'c_code','c_code',$detail->c_code,'char');
		$javascript_club = "onchange=\"changeOutlets('o_code', sectionclubs,document.adminForm.c_code.options[document.adminForm.c_code.selectedIndex].value , 0, 0);\"";
		
		$lists['published'] 	= JHTML::_('wrd.published',$detail->published,'',0 );
		$lists['projects'] 		= JHTML::_('wrd.listProjects','p_code',$filter_pid,$javascript,'p_code',0);
		$lists['clubs'] 		= JHTML::_('wrd.listClubs','c_code',$filter_cid,'','c_code');
		$lists['outlets'] 		= JHTML::_('wrd.listOutlets','o_code',$detail->o_code,'','o_code');
		//var_dump($detail->gender);
		$lists['gender'] 		= JHTML::_('wrd.gender',$detail->gender);
		
		$lists['projectToClub'] 		= JHTML::_('wrd.projectToClub');
		$lists['clubToOutlet'] 		= JHTML::_('wrd.clubToOutlet');
		
		$text = ($detail->id < 1) ? JText::_( 'NEW' ) : JText::_( 'EDIT' );
		JToolBarHelper::title(   JText::_( 'Points Manager' ).': <small><small>['.$text.']</small></small>','cpoint.png' );
		JToolBarHelper::save();
		JToolBarHelper::cancel( 'cancel', 'Close' );

		$this->assignRef('data',	$detail);
		$this->assignRef('lists',	$lists);
		$this->assignRef('user',	$user);
		parent::display($tpl);
	}
}
?>
